<template>
  <el-container class="main-layout">
    <el-header class="layout-header">
      <div class="header-content">
        <div class="logo">
          <div class="logo-icon">
            <el-icon size="28" color="#fff"><Grid /></el-icon>
          </div>
          <span class="logo-text">✨ 现代化管理系统</span>
        </div>

        <div class="header-right">
          <div class="user-welcome">
            <span class="welcome-text">欢迎回来，</span>
            <span class="username-highlight">{{ userStore.user?.username }}</span>
          </div>

          <el-dropdown @command="handleUserAction" class="user-dropdown-container">
            <div class="user-dropdown">
              <el-avatar :size="36" class="user-avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <el-icon class="dropdown-arrow"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu class="custom-dropdown">
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人资料
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  系统设置
                </el-dropdown-item>
                <el-dropdown-item command="logout" divided>
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <el-container>
      <el-aside :width="collapsed ? '64px' : '220px'" class="layout-sider">
        <div class="sider-header">
          <div class="collapse-btn" @click="collapsed = !collapsed">
            <el-icon class="collapse-icon">
              <Fold v-if="!collapsed" />
              <Expand v-else />
            </el-icon>
          </div>
        </div>

        <el-menu
          :default-active="route.name"
          background-color="transparent"
          text-color="rgba(255, 255, 255, 0.85)"
          active-text-color="#fff"
          :collapse="collapsed"
          @select="handleMenuSelect"
          class="custom-menu"
        >
          <el-menu-item index="home" class="menu-item">
            <el-icon><House /></el-icon>
            <template #title>
              <span class="menu-title">🏠 首页</span>
            </template>
          </el-menu-item>

          <el-divider class="menu-divider">
            <span v-if="!collapsed" class="divider-text">系统管理</span>
          </el-divider>

          <el-menu-item index="users" class="menu-item">
            <el-icon><User /></el-icon>
            <template #title>
              <span class="menu-title">👥 用户管理</span>
            </template>
          </el-menu-item>

          <el-menu-item index="roles" class="menu-item">
            <el-icon><Key /></el-icon>
            <template #title>
              <span class="menu-title">🔑 角色管理</span>
            </template>
          </el-menu-item>

          <el-divider class="menu-divider">
            <span v-if="!collapsed" class="divider-text">其他</span>
          </el-divider>

          <el-menu-item index="about" class="menu-item">
            <el-icon><InfoFilled /></el-icon>
            <template #title>
              <span class="menu-title">ℹ️ 关于</span>
            </template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main class="layout-content">
        <div class="content-wrapper">
          <transition name="slide" mode="out-in">
            <slot />
          </transition>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/counter'
import { ElMessageBox } from 'element-plus'
import {
  Grid,
  User,
  ArrowDown,
  Fold,
  Expand,
  House,
  Key,
  InfoFilled,
  Setting,
  SwitchButton
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const collapsed = ref(false)

const handleMenuSelect = (name: string) => {
  router.push({ name })
}

const handleUserAction = (command: string) => {
  switch (command) {
    case 'profile':
      // 跳转到个人资料页面
      break
    case 'settings':
      // 跳转到设置页面
      break
    case 'logout':
      ElMessageBox.confirm(
        '确定要退出登录吗？',
        '确认退出',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(async () => {
        await userStore.logout()
        router.push('/login')
      }).catch(() => {
        // 用户取消
      })
      break
  }
}
</script>

<style scoped>
.main-layout {
  height: 100vh;
  background: #f0f2f5;
}

.layout-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 0;
  height: 70px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1000;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 100%;
}

.logo {
  display: flex;
  align-items: center;
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.logo-icon {
  margin-right: 12px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  backdrop-filter: blur(10px);
}

.logo-text {
  background: linear-gradient(45deg, #fff, #e3f2fd);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-welcome {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  color: rgba(255, 255, 255, 0.9);
}

.welcome-text {
  font-size: 12px;
  opacity: 0.8;
}

.username-highlight {
  font-size: 14px;
  font-weight: 600;
  color: #fff;
}

.user-dropdown-container {
  cursor: pointer;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 12px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.user-dropdown:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.user-avatar {
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.user-avatar:hover {
  border-color: rgba(255, 255, 255, 0.6);
}

.dropdown-arrow {
  color: rgba(255, 255, 255, 0.8);
  transition: transform 0.3s ease;
}

.layout-sider {
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  transition: all 0.3s ease;
  box-shadow: 4px 0 20px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 999;
}

.sider-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.collapse-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  border-radius: 10px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
}

.collapse-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  transform: scale(1.1);
}

.collapse-icon {
  transition: transform 0.3s ease;
}

.layout-content {
  padding: 0;
  background: #f0f2f5;
  min-height: calc(100vh - 70px);
  overflow: hidden;
}

.content-wrapper {
  padding: 24px;
  height: 100%;
  overflow-y: auto;
}

/* 自定义菜单样式 */
.custom-menu {
  border-right: none;
  background: transparent;
  padding: 12px 8px;
}

.menu-item {
  margin: 4px 0;
  border-radius: 12px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.menu-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  transition: left 0.5s ease;
}

.menu-item:hover::before {
  left: 100%;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1) !important;
  transform: translateX(4px);
}

.menu-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.menu-item.disabled:hover {
  transform: none;
  background: transparent !important;
}

.menu-title {
  font-weight: 500;
  letter-spacing: 0.5px;
}

.menu-divider {
  margin: 16px 0;
  border-color: rgba(255, 255, 255, 0.1);
}

.divider-text {
  color: rgba(255, 255, 255, 0.6);
  font-size: 12px;
  padding: 0 12px;
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
}

/* 过渡动画 */
.slide-enter-active, .slide-leave-active {
  transition: all 0.3s ease;
}

.slide-enter-from {
  opacity: 0;
  transform: translateX(20px);
}

.slide-leave-to {
  opacity: 0;
  transform: translateX(-20px);
}

/* Element Plus 组件样式覆盖 */
:deep(.el-menu-item) {
  height: 48px;
  line-height: 48px;
  border-radius: 12px;
  margin: 4px 0;
}

:deep(.el-menu-item.is-active) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  color: #fff !important;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

:deep(.el-menu-item.is-active .el-icon) {
  color: #fff;
}

:deep(.custom-dropdown .el-dropdown-menu__item) {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

:deep(.custom-dropdown .el-dropdown-menu__item:hover) {
  background: #f0f2f5;
  color: #667eea;
}

:deep(.custom-dropdown .el-dropdown-menu__item .el-icon) {
  font-size: 16px;
}
</style>
